package cn.element.leetcode.common;

import java.util.*;

/**
 * @description:
 * @author: gonghongwei
 * @create: 2021-11-30 09:43
 */
public class Sort {

	public static void main(String[] args) {

		//1.生成1000个随机长度且数据升序的数组,并找出每个数组中最大元素
		Set<Integer> maxlist = getMaxList();
		//2.利用大顶堆排序，找出前10
		List<Integer> integers = heapSort(maxlist);
		System.out.println(Arrays.toString(integers.toArray()));
	}

	private static Set<Integer> getMaxList() {
		Set<Integer> maxlist = new HashSet<>();
		Random random = new Random();
		for (int i = 0; i < 1000; i++) {
			int size = random.nextInt(1000) + 1;
			List<Integer> list = new ArrayList<>(size);
			for (int j = 0; j < size; j++) {
				list.add(j, j + 100);
			}
			maxlist.add(list.get(size - 1));
		}
		return maxlist;
	}

	private static List<Integer> heapSort(Set<Integer> nums) {
		PriorityQueue<Integer> queue = new PriorityQueue<>((o1, o2) -> o2.intValue() - o1.intValue());
		for (Integer num : nums) {
			queue.add(num);
		}
		List<Integer> result = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			result.add(queue.poll());
		}
		return result;
	}

}
